package _struct

import (
	"fmt"
	"unsafe"
)

type myInt int

func (m myInt) say() {
	fmt.Println("Hello, 我是一个int。")
}

type intA = int //类型别名

func TestType() {
	var a myInt
	var b intA

	fmt.Printf("%T\n", a)
	fmt.Printf("%T\n", b)
}

type person struct {
	name string
	city string
	age  int
}

func NewPerson(name, city string, age int) *person {
	return &person{name, city, age}
}

//方法 有接收实例
func (p person) Dream() {
	fmt.Printf("%s的梦想是学好Go语言！\n", p.name)
}

//接受者为指针，可以改变接受者的值
func (p *person) SetAge(age int) {
	if p == nil {
		return
	}

	p.age = age
}

func (p person) SetAge2(age int) {
	p.age = age
}

type person1 struct {
	name, city string
	age        int
}

type test struct {
	a int8
	b int8
	c int8
	d int8
}

func TestStruct() {
	var p person
	p.name = "aa"
	p.city = "city"
	p.age = 18

	fmt.Printf("%v\n", p)
	fmt.Printf("%#v\n", p)

	var user struct {
		name string
		age  int
	}
	user.name = "u"
	user.age = 18

	fmt.Printf("%#v\n", user)

	var p2 = new(person)
	fmt.Printf("%#v  %T\n", p2, p2)

	p2.name = "pp"
	p2.age = 11
	p2.city = "chengdu"

	fmt.Printf("%v\n", p2)

	p3 := &person{"name", "city", 18}
	fmt.Println(p3)

	p4 := person{
		name: "aa",
		city: "cc",
		age:  18,
	}

	p5 := &person{
		name: "aa",
		city: "cc",
		age:  18,
	}
	fmt.Println(p4)
	fmt.Println(p5)

	n := test{1, 2, 3, 4}
	fmt.Printf("n.a %p\n", &n.a)
	fmt.Printf("n.b %p\n", &n.b)
	fmt.Printf("n.c %p\n", &n.c)
	fmt.Printf("n.d %p\n", &n.d)

	var v struct{}
	fmt.Println(unsafe.Sizeof(v))
	fmt.Println(unsafe.Sizeof(p))

}

type student struct {
	name string
	age  int
}

func newStudent(name string, age int) *student {
	return &student{name, age}
}

func TestStruct2() {
	m := make(map[string]*student)

	stus := []student{
		student{"1", 1},
		student{"2", 2},
		student{"3", 3},
	}

	for _, stu := range stus {
		m[stu.name] = &stu
	}
	for k, v := range m {
		fmt.Println(k, v)
	}

	m = make(map[string]*student)
	for _, stu := range stus {
		m[stu.name] = newStudent(stu.name, stu.age)
	}
	for k, v := range m {
		fmt.Println(k, v)
	}

	p2 := NewPerson("aa", "cc", 18)
	p2.Dream()
	p2.SetAge(30)
	fmt.Println(p2)

	p2.SetAge2(20)
	fmt.Println(p2)
}
